/*
Copyright(c) 2008 Jari Kemppinen <jari@viddem.com>

This example was built upon the PureMVC Framework by Cliff Hall.
http://www.puremvc.org
This example uses the Java PureMVC Framework Version 0.2

Please contribute to the PureMVC community.

License
-------
Your reuse is governed by the Creative Commons Attribution 3.0 license.
 */

package org.lbroussal.client.controller;

import org.lbroussal.client.ApplicationFacade;
import org.lbroussal.client.model.ItemDataProxy;
import org.lbroussal.client.model.vo.ItemDataVO;
import org.lbroussal.client.model.vo.UserVO;
import org.puremvc4gwt.client.interfaces.ICommand;
import org.puremvc4gwt.client.interfaces.INotification;
import org.puremvc4gwt.client.patterns.command.SimpleCommand;

import com.google.gwt.user.client.Window;

public class ProcessLogin extends SimpleCommand<ApplicationFacade> implements ICommand {


    private String[] url = new String[] { "/puremvc.png", "/java.png" };

    private String[] data = new String[] { "PureMVC", "Java J2ME" };

    public void execute(INotification notification) {
        String[] params = (String[]) notification.getBody();

        doLogin(params[0], params[1]);
    }

    public void doLogin(String userId, String password) {
        if (!userId.equals("puremvc") && !password.equals("4gwt")) {
            Window.alert("Valid credential are puremvc/4gwt, try again please.");
            if(facade.getUserDataScope().incrementAndGetLoginFailCount() == 3) {
                this.notifyObservers(ApplicationFacade.LOGIN_FAIL);
            }
            return;
        } else {
            facade.getUserDataScope().resetLoginFailCount();
        }

        // Login succeeded
        final UserVO user = new UserVO();
        user.setFirstName("Luciano");
        user.setFirstName("Broussal");
        facade.getUserDataScope().setData(user);

        // Add some ItemDataVO objects using proxy
        ItemDataProxy itemProxy = facade.getItemDataScope();
        for (int i = 0; i < url.length; i++) {
            itemProxy.addItem(new ItemDataVO(url[i], data[i]));
        }

        this.notifyObservers(ApplicationFacade.LOGIN_SUCCESSFUL);
    }
}